package com.blog.controller.admin;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.blog.entity.Comment;
import com.blog.entity.PageBean;
import com.blog.service.CommentService;
import com.blog.util.DateJsonValueProcessor;
import com.blog.util.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping({"/admin/comment"})
public class CommentAdminController {
	
	@Resource
	private CommentService commentService;
	
	/**
	 * 查询评论列表
	 */
	@RequestMapping({"list"})
	public String list(@RequestParam(value="page",required=false)String page,
			@RequestParam(value="rows",required=false)String rows,
			@RequestParam(value="state",required=false)String state,
			HttpServletResponse response) throws IOException{
		PageBean pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		Map<String,Object> map = new HashMap<>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		map.put("state", state);
		// 查询评论列表
		List<Comment> commentList = commentService.list(map);
		// 查询总共有多少条数据
		Long total = commentService.getTotal(map);
		//将数据写入response
		JSONObject result = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(commentList,jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 删除评论
	 */
	@RequestMapping({"/delete"})
	public String delete(@RequestParam("ids")String ids,HttpServletResponse response) throws IOException {
		String[] idsStr = ids.split(",");
		JSONObject result = new JSONObject();
		for(int i = 0;i<idsStr.length;i++) {
			commentService.delete(Integer.parseInt(idsStr[i]));
		}
		result.put("success", Boolean.valueOf(true));
		ResponseUtil.write(response, result);
		return null;
		
	}
	
	/**
	 * 审核评论
	 */
	@RequestMapping({"/review"})
	public String review(@RequestParam("ids")String ids,@RequestParam(value="state",required=false)String state,HttpServletResponse response) throws IOException {
		String[] idsStr = ids.split(",");
		JSONObject result = new JSONObject();
		for(int i = 0;i<idsStr.length;i++) {
			Comment comment = new Comment();
			comment.setId(Integer.valueOf(idsStr[i]));
			comment.setState(Integer.valueOf(state));
			
			commentService.update(comment);
		}
		result.put("success", Boolean.valueOf(true));
		ResponseUtil.write(response, result);
		return null;
		
	}
}
